var util = require('../../../utils/util')
var api = require('../../../config/api.js');
const innerAudioContext = wx.createInnerAudioContext()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    task: {},
    imgListview:[],
    imgList: [],
    tasklog:{
      taskId:null,
      remark:null,
      fliePath:[]
    },
    taskLogList:[],
    
    verifyResult:'1',
    taskRemark:null,
    taskId:null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('row', function (data) {
      that.setData({
        taskId:data.taskId
      })
      //获取任务详情
      that.getTaskById(data.taskId)
      //获取上报历史
      that.getLogByTaskId(data.taskId)
    })

  },

   //获取任务详情
   getTaskById(taskId) {
    let that = this
    let dataup = {
      taskId: taskId
    }
    util.request(api.queryTaskDetail, JSON.stringify(dataup), 'POST').then(function (res) {
      if (res.code == '200') {
        that.closeModal();
        that.setData({
          task: res.data
        })

      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

  },
  
   //获取任务上报列表
   getLogByTaskId(taskId) {
    let that = this
    let dataup = {
      taskId: taskId,
      isIdentify:'1'
    }
    util.request(api.taskLogListById, JSON.stringify(dataup), 'POST').then(function (res) {
      console.log(res)
      if (res.code == '200') {
        that.closeModal();
        that.setData({
          taskLogList: res.data
        })

      } else {
        util.showErrorToast(res.message);
        that.closeModal();
      }

    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

  },


    //隐患描述
    hiddenRemarkInput(event){
      this.data.taskRemark=event.detail.value
    },
    //失败或成功
    switchChange(event){
      if(event.detail.value){
        this.data.verifyResult=1
      }else{
        this.data.verifyResult=0
      }
    },

 
//浏览图片
ViewImage(e) {
  wx.previewImage({
    urls: this.data.imgList,
    current: e.currentTarget.dataset.url
  });
},

//浏览图片
ViewImageView(e) {
  console.log(e)
    wx.previewImage({
      urls: e.currentTarget.dataset.data.fliePath,
      current: e.currentTarget.dataset.data.fliePath[0]
    });
  },

  //日志列表
  taskloglist() {
    wx.navigateTo({
      url: '/pages/tasklog/taskloglist?taskId=' + this.data.taskId
    })

  },
  //上报日志
  submittask() {  
  //提交上报数据
 
  let dataup={
    taskId:this.data.taskId,
    verifyResult:this.data.verifyResult,
    taskRemark:this.data.taskRemark
  };
  let that=this;
  console.log(JSON.stringify(dataup))
  that.loadModal();
  util.request(api.taskJudge,JSON.stringify(dataup),'POST').then(function (res) {
      console.log(res)
      if (res.code == 200) {
          that.closeModal();
          //刷新界面
          wx.navigateBack({})
      
      } else {
        util.showErrorToast(res.message);
      }
      
    }).catch((err) => {
      that.closeModal();
      console.log("登录远程服务器catch:" + err);
      //reject(err);
    });

  },

  
//进度条
loadModal () {
  this.setData({
    loadModal: true
  })
},
closeModal (){
  this.setData({
    loadModal: false
  })
},

})